package com.atguigu.ting.realtime.util;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.ting.realtime.common.Constant;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava30.com.google.common.base.CaseFormat;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lzc
 * @Date 2023/3/10 10:18
 */
public class JdbcUtil {
    
    public static Connection getPhoenixConnection() {
        String driver = Constant.PHOENIX_DRIVER;
        String url = Constant.PHOENIX_URL;
        return JdbcUtil.getJdbcConnection(driver, url, null, null);
    }
    
    private static Connection getJdbcConnection(String driver, String url, String user, String password) {
        
        // 1. 加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 2. 使用DriverManager 获取链接
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static Connection getMysqlConnection() {
        
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        
        return getJdbcConnection(driver, url, "root", "aaaaaa");
    }
    
    
    public static <T> List<T> queryList(Connection conn,
                                        String querySQL,  // select * from aa
                                        Object[] args,
                                        Class<T> tClass,
                                        boolean isUnderlineToCamel) {
        List<T> list = new ArrayList<>();
        try {
            PreparedStatement ps = conn.prepareStatement(querySQL);
            // 给 sql 中的占位符进行赋值
            for (int i = 0; args != null && i < args.length; i++) {
                Object arg = args[i];
                ps.setObject(i + 1, arg);
            }
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 对结果进行解析
            while (resultSet.next()) {
                // 有一行数据: 把这行数据封装到一个T 类型的对象中, 再把这对象放入到 List 集合中
                T t = tClass.newInstance();  // 使用无参构造器创建对象
                // T对象中, 封装每一列的数据  // User(name, age)
                // 把查到的所有列存入到 T 对象中
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // 拿到列名
                    String name = metaData.getColumnLabel(i);
                    if (isUnderlineToCamel) {
                        // aa_bb => aaBb  ( 变量名 方法名)
                        // aa_bb =>AaBb (类名)
                        name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
                    }
                    Object value = resultSet.getObject(i);
                    //t.name=value;  t.setName(value)
                    BeanUtils.setProperty(t, name, value);
                }
                list.add(t);
            }
            ps.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        return list;
    }
    
    public static void main(String[] args) {
        Connection conn = getPhoenixConnection();
        List<JSONObject> list = queryList(conn, "select * from dim_base_trademark where id=?", new Object[]{"1"}, JSONObject.class, false);
        
//        List<JSONObject> list = queryList(conn, "select * from gmall2022.sku_info where id=?", new Object[]{10}, JSONObject.class);
//        List<TableProcess> list = queryList(conn, "select * from gmall_config.table_process", new Object[]{}, TableProcess.class, true);
        
        for (Object obj : list) {
            System.out.println(obj);
        }
    
    }
    
}
